package com.wut.jmx.controller;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wut.jmx.common.R;
import com.wut.jmx.entity.Category;
import com.wut.jmx.entity.Employee;
import com.wut.jmx.service.CategoryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@Slf4j
@Api(tags = "分类管理")
@RequestMapping("/category")
public class CategoryController {
    @Autowired
    CategoryService categoryService;
    @ApiOperation("分页查询")
    @GetMapping("/page")
    public R<Page<Category>> page(Integer page,Integer pageSize,String name) {
        log.info("page:{},pageSize:{},name:{}",page,pageSize,name);
        //构建分页对象
        Page pageInfo = new Page(page,pageSize);
        LambdaQueryWrapper<Category> queryWrapper = new LambdaQueryWrapper<>();
//        queryWrapper.like(StringUtils.isNotBlank(name),Category::getName,name);
        Page result = categoryService.page(pageInfo, queryWrapper);
        return R.success(result);
    }
    @ApiOperation("根据id修改菜品分类信息")
    @PutMapping()
    public R<Category> updateById(HttpServletRequest request, @RequestBody Category updateCategory){
        log.info("update employee:{}",updateCategory);
        boolean update = categoryService.updateById(updateCategory);
        if (update){
            return R.success(updateCategory);
        }
        else {
            return R.error("失败");
        }
    }
    @ApiOperation("根据id删除菜品信息")
    @DeleteMapping()
    public R<String> delete(@RequestParam("id") Long id) { //注意这里前端传过来的数据是ids
        categoryService.remove(id);
        return R.success("分类信息删除成功");
    }
    @ApiOperation("新增分类")
    @PostMapping()
    public R<String> save( HttpServletRequest request,@RequestBody Category saveCategory){
       categoryService.save(saveCategory);
       return R.success("保存成功");
    }
    @ApiOperation("菜品分类-菜品管理")
    @GetMapping("/list")
    public R<List<Category>> list(Category category){
        LambdaQueryWrapper<Category> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(category.getType() != null,Category::getType,category.getType());
        List<Category> list = categoryService.list(queryWrapper);
        return R.success(list);
    }
}
